package com.syezon.wifikey.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelpay.PayResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        overridePendingTransition(0, 0);
        try {
            api = WXAPIFactory.createWXAPI(this, WXConstant.APP_ID, false);
            Intent intent = getIntent();
            boolean handleIntent = api.handleIntent(intent, this);
            finish();
        } catch (Exception e) {
            e.printStackTrace();
            finish();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        if (api != null) {
            api.handleIntent(intent, this);
        }
        finish();
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, 0);
    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp resp) {
        try {
            if (resp != null && resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
                if (resp instanceof PayResp) {
                    PayResp payResp = (PayResp) resp;
                    int errCode = payResp.errCode;
                    if (errCode == 0) {
                    } else if (errCode == -2) {
                    } else {
                    }
                    if (payResp.prepayId != null) {
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
